<script setup>
import { artGetChannelsService } from '@/api/article'
import { Delete, Edit } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'

import { artDelChannelsService } from '@/api/article'
const channelList = ref([])

import ChannelEdit from './components/ChannelEdit.vue'
import { ElMessageBox } from 'element-plus'


// 控制页面渲染前加载画面
const loading = ref(false)

// 获取组件Dom元素，同名ref属性
const dialog = ref()


const getChannelList = async () => {
  loading.value = true
  const { data } = await artGetChannelsService()
  channelList.value = data.data
  // channelList.value = []
  loading.value = false
}
getChannelList()
// onMounted(() => getChannelList())

// 编辑按钮
const onEditChannel = (row) => {
  console.log(row)
  // console.log($index)
  dialog.value.open(row)
}
// 删除按钮
const onDelChannel = async (row) => {
  // 再次确认
  await ElMessageBox.confirm('请确认是否删除', '提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })

  await artDelChannelsService(row.id)
  ElMessage({
    type: 'success',
    message: '删除成功'
  })
  getChannelList()
}
// 添加分类
const onAddChannel = () => {
  dialog.value.open({})
}

// 重新渲染页面
const onSuccess = () => {
  getChannelList()
}

</script>

<template>
  <!-- 给组件添加静态属性title,父传子单项 -->
  <page-container title="文章分类">
    <template></template>
    <!-- 对应组件name="extra"的具名插槽 -->
    <template #extra>
      <el-button @click="onAddChannel" type="primary">添加分类</el-button>
    </template>
    <!-- 表格部分 -->
    <el-table v-loading="loading" :data="channelList" style="width: 100%;">
      <!-- 组件提供索引列声明 -->
      <el-table-column type="index" label="序号" width="150"></el-table-column>
      <!-- 通过prop传递对象数组属性给索引列单元格提供数据 -->
      <el-table-column prop="cate_alias" label="分类名称"></el-table-column>
      <el-table-column prop="cate_name" label="分类别名"></el-table-column>
      <el-table-column label="操作">
        <!-- 通过组件提供的默认插槽，自定义按钮 -->
        <template #default="{ row }">
          <el-button plain round type="primary" :icon="Edit" @click="onEditChannel(row)"></el-button>
          <el-button plain round type="danger" :icon="Delete" @click="onDelChannel(row)"></el-button>
        </template>
      </el-table-column>

      <!-- 数据为空时渲染 -->
      <!-- table的empty插槽,数据为空时渲染 -->
      <template #empty>
        <el-empty description="未添加分类"></el-empty>
      </template>
    </el-table>
  </page-container>

  <!-- 封装的弹框组件 -->
  <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
</template>
